﻿==============================================================================
DOSYA ADI  : MessageBoxUIF.cpp
KAYNAK      :-Pearl Guard\MessageBoxUIF.cpp
SISTEM      : v2369 Reconnect - ISTEMCI (Pearl Guard)
BOYUT       : 7865 bayt / 302 satir
ACIKLAMA    : Reconnect UI mesaj kutusu / onay penceresi islemleri
==============================================================================

--- DOSYA ICERIGI ASAGIDAKI GIBIDIR ------------------------------------

#include "stdafx.h"
#include "MessageBoxUIF.h"

extern void SendReconnectResume();
extern void SendReconnectStay();
extern void SendReconnectDisconnectNow();
extern void ReleaseReconnectBridge();
extern bool IsReconnectUiHoldMode();
extern bool IsReconnectHoldExpired();
extern volatile LONG reconnectBridgeActive;
extern bool reconnectUiShown;

CUIMessageBox::CUIMessageBox(MsgBoxTypes type, ParentTypes parentType)
{
	m_Type = type;
	m_ParentType = parentType;

	m_txtTitle = NULL;
	m_txtMessage = NULL;

	m_editTextbox = NULL;

	m_btnNo = NULL;
	m_btnYes = NULL;
	m_btnOk = NULL;
}

CUIMessageBox::~CUIMessageBox()
{
}
 
bool CUIMessageBox::Load(HANDLE hFile)
{
	if (CN3UIBase::Load(hFile) == false) return false;

	std::string find = xorstr("Text_title");
	m_txtTitle = (CN3UIString*)GetChildByID(find);
	find = xorstr("Text_Message");
	m_txtMessage = (CN3UIString*)GetChildByID(find);

	find = xorstr("Edit_Common");
	m_editTextbox = (CN3UIEdit*)GetChildByID(find);

	find = xorstr("btn_yes");
	m_btnYes = (CN3UIButton*)GetChildByID(find);
	find = xorstr("btn_no");
	m_btnNo = (CN3UIButton*)GetChildByID(find);
	find = xorstr("btn_ok");
	m_btnOk = (CN3UIButton*)GetChildByID(find);

	if (m_Type == Ok || m_Type == OkWithEdit)
	{
		m_btnYes->SetVisible(false);
		m_btnNo->SetVisible(false);
		m_btnOk->SetVisible(true);
		SetVisibleTextbox(m_Type == OkWithEdit);
	}
	else if (m_Type == YesNo || m_Type == YesNoWithEdit)
	{
		m_btnYes->SetVisible(true);
		m_btnNo->SetVisible(true);
		m_btnOk->SetVisible(false);
		SetVisibleTextbox(m_Type == YesNoWithEdit);
	}
	else if (m_Type == Cancel)
	{
		m_btnYes->SetVisible(false);
		m_btnNo->SetVisible(false);
		m_btnOk->SetVisible(false);
		SetVisibleTextbox(false);
	}

	SetPos(Engine->m_UiMgr->GetScreenCenter(this).x, Engine->m_UiMgr->GetScreenCenter(this).y);

	return true;
}

void CUIMessageBox::Update(MsgBoxTypes type, ParentTypes parentType)
{
	m_Type = type;
	m_ParentType = parentType;

	if (m_Type == Ok || m_Type == OkWithEdit)
	{
		m_btnYes->SetVisible(false);
		m_btnNo->SetVisible(false);
		m_btnOk->SetVisible(true);
		SetVisibleTextbox(m_Type == OkWithEdit);
	}
	else if (m_Type == YesNo || m_Type == YesNoWithEdit)
	{
		m_btnYes->SetVisible(true);
		m_btnNo->SetVisible(true);
		m_btnOk->SetVisible(false);
		SetVisibleTextbox(m_Type == YesNoWithEdit);
	}
	else if (m_Type == Cancel)
	{
		m_btnYes->SetVisible(false);
		m_btnNo->SetVisible(false);
		SetVisibleTextbox(false);
		m_btnOk->SetVisible(false);
	}

	SetPos(Engine->m_UiMgr->GetScreenCenter(this).x, Engine->m_UiMgr->GetScreenCenter(this).y);
}

void CUIMessageBox::SetTitle(std::string title)
{
	m_txtTitle->SetString(title);
}

void CUIMessageBox::SetMessage(std::string msg)
{
	m_txtMessage->SetString(msg);
}

void CUIMessageBox::SetVisibleTextbox(bool bVisible)
{
	if (NULL == m_editTextbox) return;

	m_editTextbox->SetString("");
	m_editTextbox->SetVisible(bVisible);
	if (bVisible) m_editTextbox->SetFocus();
	else m_editTextbox->KillFocus();
}
bool CUIMessageBox::ReceiveMessage(CN3UIBase* pSender, uint32_t dwMsg)
{
	if (dwMsg == UIMSG_BUTTON_CLICK)
	{
		if (pSender == m_btnYes)
		{
			if (m_ParentType == PARENT_PAGE_STATE)
			{
				Packet result(XSafe);
				result << uint8_t(XSafeOpCodes::RESET) << uint8(2);
				Engine->Send(&result);
			}
			else if (m_ParentType == PARENT_SKILL_TREE)
			{
				Packet result(XSafe);
				result << uint8_t(XSafeOpCodes::RESET) << uint8(1);
				Engine->Send(&result);
			}
			else if (m_ParentType == PARENT_PERK_RESET) {
				Packet result(XSafe, uint8(XSafeOpCodes::PERKS));
				result << uint8(perksSub::perkReset);
				Engine->Send(&result);
			}
			else if (m_ParentType == PARENT_RECONNECT)
			{
				if (IsReconnectHoldExpired())
				{
					// Süre doldu: hiçbir seçenek oyunda tutmaz; karakter
					// oyundan düşürülür (panel sadece kapanır).
				}
				else if (InterlockedCompareExchange(&reconnectBridgeActive, 0, 0) != 0)
				{
					// Köprü aktif (bağlantı koptu) → hemen yeniden bağlan.
					SendReconnectResume();
				}
				else if (IsReconnectUiHoldMode())
				{
					// Test beklemesi → "Yes" = oyunda kal (planlı disconnect iptal).
					SendReconnectStay();
				}
				else
				{
					// Bağlantı koptu → yeniden bağlan.
					SendReconnectResume();
				}
			}
		}
		else if (pSender == m_btnNo)
		{
			if (m_ParentType == PARENT_RECONNECT)
			{
				if (IsReconnectHoldExpired())
				{
					// Süre doldu: seçenek yok, karakter oyundan düşürülür.
				}
				else if (InterlockedCompareExchange(&reconnectBridgeActive, 0, 0) != 0)
				{
					// Köprüdeyiz → normal disconnect akışına dön.
					ReleaseReconnectBridge();
				}
				else if (IsReconnectUiHoldMode())
				{
					// Test beklemesi → "No" = disconnect'i hemen uygula.
					SendReconnectDisconnectNow();
				}
				// Bağlantı koptu modunda "No": hiçbir şey yapma (bağlantıda kalma).
			}
		}
		else if (pSender == m_btnOk)
		{
			if (m_ParentType == PARENT_LOGIN)
			{
				// UYARI GİDERİLDİ: 'exit(0)' sonrası ulaşılamayan kodlar silindi
				exit(0);
			}
		}

		Close();
		if (m_ParentType == PARENT_RECONNECT)
			reconnectUiShown = false;
	}

	return true;
}

bool CUIMessageBox::OnKeyPress(int iKey)
{
	if (!IsVisible())
		return CN3UIBase::OnKeyPress(iKey);

	if (m_Type == Ok || m_Type == OkWithEdit)
	{
		if (iKey == DIK_ESCAPE || iKey == DIK_RETURN)
		{
			ReceiveMessage(m_btnOk, UIMSG_BUTTON_CLICK);
			return true;
		}
	}
	else if (m_Type == YesNo || m_Type == YesNoWithEdit)
	{
		if (iKey == DIK_ESCAPE)
		{
			ReceiveMessage(m_btnNo, UIMSG_BUTTON_CLICK);
			return true;
		}
		else if (iKey == DIK_RETURN)
		{
			ReceiveMessage(m_btnYes, UIMSG_BUTTON_CLICK);
			return true;
		}
	}
	else if (m_Type == Cancel)
	{
		if (iKey == DIK_ESCAPE)
		{
			ReceiveMessage(m_btnNo, UIMSG_BUTTON_CLICK);
			return true;
		}
	}

	return CN3UIBase::OnKeyPress(iKey);
}

void CUIMessageBox::Close()
{

	m_Type = None;
	m_ParentType = PARENT_NONE;

	this->SetVisible(false);
	this->SetMessage("");
	this->SetTitle("");
	this->SetVisibleTextbox(false);

	Engine->m_UiMgr->RemoveChild(this);
	Engine->m_UiMgr->uiMsgBox->Release();
	Engine->m_UiMgr->uiMsgBox = NULL;
}


uint8 PearlEngine::GetNation()
{
	return *(uint8*)(*(DWORD*)KO_PTR_CHR + KO_OFF_NATION);
}

void CUIMessageBox::OpenMessageBox(MsgBoxTypes type, ParentTypes parent)
{
#if (HOOK_SOURCE_VERSION == 1098)
	uint8 Nation = *(uint8*)(*(DWORD*)KO_PTR_CHR + KO_OFF_NATION);
	if (Nation == 1)
	{
		std::string name = Engine->dcpUIF(xorstr("BuhuR\\Ka_MessageBox.bbuhurr"));
		Engine->m_UiMgr->uiMsgBox = new CUIMessageBox(type, parent);
		Engine->m_UiMgr->uiMsgBox->Init(this);
		Engine->m_UiMgr->uiMsgBox->LoadFromFile(Engine->m_BasePath + name.c_str(), N3FORMAT_VER_1068);
		Engine->m_UiMgr->AddChild(Engine->m_UiMgr->uiMsgBox);
		remove(name.c_str());
}
	else if (Nation == 2)
	{
		std::string name = Engine->dcpUIF(xorstr("BuhuR\\El_MessageBox.bbuhurr"));
		Engine->m_UiMgr->uiMsgBox = new CUIMessageBox(type, parent);
		Engine->m_UiMgr->uiMsgBox->Init(this);
		Engine->m_UiMgr->uiMsgBox->LoadFromFile(Engine->m_BasePath + name.c_str(), N3FORMAT_VER_1068);
		Engine->m_UiMgr->AddChild(Engine->m_UiMgr->uiMsgBox);
		remove(name.c_str());
	}
#else
	std::string name = Engine->dcpUIF(xorstr("BuhuR\\re_message_box.bbuhurr"));
	Engine->m_UiMgr->uiMsgBox = new CUIMessageBox(type, parent);
	Engine->m_UiMgr->uiMsgBox->Init(this);
	Engine->m_UiMgr->uiMsgBox->LoadFromFile(Engine->m_BasePath + name.c_str(), N3FORMAT_VER_1068);
	Engine->m_UiMgr->AddChild(Engine->m_UiMgr->uiMsgBox);
	remove(name.c_str());
#endif
}